package com.lanou.dllo.shise.com.lanou.dllo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.shise.R;
import com.lanou.dllo.shise.com.lanou.dllo.entity.ChooseBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/11/4.
 */
public class Carefully_chooseAdapter extends RecyclerView.Adapter<Carefully_chooseAdapter.ViewHolder>{
    private RequestQueue queue;
    private List<ChooseBean.ResultEntity.ListEntity> listEntities;
    public Carefully_chooseAdapter(Context context) {
        queue = Volley.newRequestQueue(context);
    }
    public void addData(List<ChooseBean.ResultEntity.ListEntity> datas){
        listEntities = datas;
        notifyDataSetChanged();
    }

    @Override
    public Carefully_chooseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recycler_view_carefully_chosen,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(Carefully_chooseAdapter.ViewHolder holder, int position) {
        if (listEntities != null){
            ChooseBean.ResultEntity.ListEntity list = listEntities.get(position);
            holder.carefully_choosen_cooking.setText(list.getCooking());
            holder.carefully_choosen_favourite.setText(list.getFavourite());
            holder.carefully_choosen_title.setText(list.getTitle());
            if (list.getImage()!= null && list.getImage().length() > 0){
                String url = list.getImage();
                getBitmap(url,holder.carefully_choosen_image);

            }
        }

    }

    private void getBitmap(String url, final ImageView carefully_choosen_image) {
        ImageRequest pictureRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                carefully_choosen_image.setImageBitmap(response);
            }
        }, 720, 1280, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");//添加方法头  避免出现缓存问题
                return super.getHeaders();
            }
        };
        queue.add(pictureRequest);
    }

    @Override
    public int getItemCount() {
        if (listEntities != null){
            return listEntities.size();
        }
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView carefully_choosen_image;
        private TextView carefully_choosen_title,carefully_choosen_favourite,
                carefully_choosen_cooking;

        public ViewHolder(View v) {
            super(v);
            carefully_choosen_image = (ImageView) v.findViewById(R.id.carefully_choosen_image);
            carefully_choosen_title = (TextView) v.findViewById(R.id.carefully_choosen_title);
            carefully_choosen_cooking = (TextView) v.findViewById(R.id.carefully_choosen_cooking);
            carefully_choosen_favourite = (TextView) v.findViewById(R.id.carefully_choosen_favourite);

        }
    }
}
